// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import iView, {Message} from 'iview'
import VueQuillEditor from 'vue-quill-editor'
import axios from 'axios'
import 'iview/dist/styles/iview.css'
import './main.css'
import Vuex from 'vuex'

Vue.use(iView)
Vue.use(Vuex)

Vue.use(VueQuillEditor)

axios.interceptors.request.use(
  config => {
    if (localStorage.getItem('token')) {
      config.headers.Authorization = localStorage.getItem('token')
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 拦截器
axios.interceptors.response.use(result => {
  // 当验证没登录时
  if (result.data.code === 401) {
    router.push('/login')
    localStorage.removeItem('token')
  }
  if (result.data.code === 403) {
    Message.warning(result.data.message)
  }
  return result
}, err => {
  // message.error('接口出错了!!!');
  return Promise.reject(err)
})

let store = new Vuex.Store({
  state: {
    userInfo: {}
  },
  getters: {
    qx: (state) => {
      console.log(state)
      if (state.userInfo.data) {
        return state.userInfo.data.role.authority
      } else {
        return []
      }
    }
  },
  mutations: {
    INCREMENT (state, res) {
      state.userInfo = res.data
    }

  },
  actions: {
    getser (context, id) {
      localStorage.setItem('id', id)
      axios.get('api/users/info').then(res => {
        context.commit('INCREMENT', res)
      })
    }

  }

})

Vue.config.productionTip = false
Vue.prototype.$axios = axios

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: {App},
  template: '<App/>'
})
